<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EasyQtSql: EasyQtSql User Manual</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">EasyQtSql
   </div>
   <div id="projectbrief">Easy SQL data access helper for QtSql</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('index.htm','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title"><a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> User Manual </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="intro_sec"></a>
Introduction</h1>
<p><a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> is a lightweight header-only library for quick and easy SQL querying with QtSql classes.</p>
<h1><a class="anchor" id="install_sec"></a>
Quick Start guide</h1>
<h2><a class="anchor" id="step1"></a>
Step 1: Create Qt application project</h2>
<p>You can use QtCreator to create a simple minimal console application with a single main.cpp file.</p>
<p>Or use a ready-made application where you want to add database support.</p>
<h2><a class="anchor" id="step2"></a>
Step 2: Add QtSql support</h2>
<p>Open *.pro file and add QtSql module (QT += sql):</p>
<div class="fragment"><div class="line">QT += sql</div><div class="line">QT -= gui</div><div class="line"></div><div class="line">CONFIG += c++11</div><div class="line"></div><div class="line">TARGET = App</div><div class="line">CONFIG += console</div><div class="line">CONFIG -= app_bundle</div><div class="line"></div><div class="line">TEMPLATE = app</div></div><!-- fragment --><h2><a class="anchor" id="step3"></a>
Step 3: Include EasyQtSql library</h2>
<p>To use my library you need to include single header file (<a class="el" href="_easy_qt_sql_8h.htm" title="Main EasyQtSql header file. ">EasyQtSql.h</a>) in your source code. So you need to put the library header files (<a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> folder) in an accessible place next to your project and to specify path to the <a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> folder with INCLUDEPATH directive. You can just include EasyQtSql.pri inside your *.pro file:</p>
<div class="fragment"><div class="line">QT += sql</div><div class="line">QT -= gui</div><div class="line"></div><div class="line">include(../<a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>/<a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>.pri)</div><div class="line"></div><div class="line">CONFIG += c++11</div><div class="line"></div><div class="line">TARGET = App</div><div class="line">CONFIG += console</div><div class="line">CONFIG -= app_bundle</div><div class="line"></div><div class="line">TEMPLATE = app</div><div class="line"></div><div class="line"><span class="preprocessor">#INCLUDEPATH += ../EasyQtSql</span></div><div class="line"></div><div class="line">SOURCES += <a class="code" href="dummy_8cpp.htm#a0ddf1224851353fc92bfbff6f499fa97">main</a>.cpp \
    TestObject.cpp</div><div class="line"></div><div class="line">HEADERS += \</div><div class="line">    TestObject.h     </div></div><!-- fragment --><h2><a class="anchor" id="step4"></a>
Step 4: Include the library header file and start using it</h2>
<p>Include <a class="el" href="_easy_qt_sql_8h.htm" title="Main EasyQtSql header file. ">EasyQtSql.h</a> header file and start using the library.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;QCoreApplication&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="_easy_qt_sql_8h.htm">EasyQtSql.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;TestObject.h&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> <a class="code" href="dummy_8cpp.htm#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">   QCoreApplication a(argc, argv);</div><div class="line"></div><div class="line">   qDebug() &lt;&lt; <span class="stringliteral">&quot;begin&quot;</span>;</div><div class="line"></div><div class="line">   QSqlDatabase sdb = QSqlDatabase::addDatabase(<span class="stringliteral">&quot;QSQLITE&quot;</span>);</div><div class="line">   sdb.setDatabaseName(<span class="stringliteral">&quot;:memory:&quot;</span>);</div><div class="line"></div><div class="line">   <span class="keywordflow">try</span></div><div class="line">   {</div><div class="line">      <a class="code" href="class_easy_qt_sql_1_1_transaction.htm">Transaction</a> t(sdb);</div><div class="line"></div><div class="line">      t.<a class="code" href="class_database.htm#aae9f7e53275334c91655eeb7a2bde030">execNonQuery</a>(<span class="stringliteral">&quot;CREATE TEMP TABLE IF NOT EXISTS table1 (a int, b int, c int, d text)&quot;</span>);</div><div class="line"></div><div class="line">      <span class="comment">//multi insert</span></div><div class="line">      <a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm">NonQueryResult</a> res1 = t.<a class="code" href="class_database.htm#a8388689590c1f3adeabd2972b221bcb2">insertInto</a>(<span class="stringliteral">&quot;table1 (a, b, c, d)&quot;</span>)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(1, 2, 3, 7)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(2, 3, 4, <span class="stringliteral">&quot;test&quot;</span>)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(2, 3, 4, 8)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#a273196e89e6fa6607cba428bd0a91a71">exec</a>();</div><div class="line"></div><div class="line">      qDebug() &lt;&lt; res1.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#accc70f0c2199629ce062feb8de0c2fe7">lastQuery</a>();</div><div class="line"></div><div class="line">      <span class="comment">//delete with condition</span></div><div class="line">      <span class="comment">//t.deleteFrom(&quot;table1&quot;).where(&quot;a = ? AND b = ?&quot;, 1, 2);</span></div><div class="line"></div><div class="line">      <span class="comment">//delete without condition</span></div><div class="line">      <span class="comment">//t.deleteFrom(&quot;table1&quot;).exec();</span></div><div class="line"></div><div class="line">      <span class="comment">//delete with constant condition</span></div><div class="line">      <span class="comment">//t.deleteFrom(&quot;table1&quot;).where(&quot;1=0&quot;);</span></div><div class="line">      <a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm">PreparedQuery</a> query = t.<a class="code" href="class_database.htm#a03e2c9113452bb52bb7018ef2366bce1">prepare</a>(<span class="stringliteral">&quot;SELECT a, b, c, d FROM table1 WHERE a = ? AND b = ?&quot;</span>);</div><div class="line"></div><div class="line">      <a class="code" href="class_easy_qt_sql_1_1_query_result.htm">QueryResult</a> res = query.<a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm#a8b37cea455273109305cb8e621b169bf">exec</a>(1, 2);</div><div class="line"></div><div class="line">      qDebug() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#abe5f540e13e57385fc57c8826b61816e">executedQuery</a>() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(0) &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(1);</div><div class="line"></div><div class="line">      <span class="keywordflow">while</span>(res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a3e1b0790cdb82a848f2b48ddc698001b">next</a>())</div><div class="line">      {</div><div class="line">         QVariantMap map = res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a27809ada6725d479f31b66ebcbc7c95c">toMap</a>();</div><div class="line"></div><div class="line">         qDebug() &lt;&lt; map;</div><div class="line">      }</div><div class="line"></div><div class="line">      res = query.<a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm#a8b37cea455273109305cb8e621b169bf">exec</a>(2, 3);</div><div class="line"></div><div class="line">      qDebug() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#abe5f540e13e57385fc57c8826b61816e">executedQuery</a>() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(0) &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(1);</div><div class="line"></div><div class="line">      <span class="keywordflow">while</span>(res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a3e1b0790cdb82a848f2b48ddc698001b">next</a>())</div><div class="line">      {</div><div class="line">         QStringList list = res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a152e0d75c2c149896955c3257ca08e7d">toStringList</a>();</div><div class="line">         qDebug() &lt;&lt; list;</div><div class="line"></div><div class="line">         TestObject obj;</div><div class="line"></div><div class="line">         <span class="keywordtype">int</span> a;</div><div class="line">         <span class="keywordtype">bool</span> b;</div><div class="line">         <span class="keywordtype">double</span> c;</div><div class="line">         QVariant d;</div><div class="line">         QByteArray arr;</div><div class="line"></div><div class="line">         res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#ab0dac0809b8eab8f066106391346dfb4">fetchVars</a>(a, b, c, d, arr);</div><div class="line"></div><div class="line">         res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a043b284c72b54c4a2871867196a67560">fetchObject</a>(obj);</div><div class="line"></div><div class="line">         qDebug() &lt;&lt; a &lt;&lt; b &lt;&lt; c &lt;&lt; d;</div><div class="line">      }</div><div class="line"></div><div class="line">      <a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm">NonQueryResult</a> res2 = t.<a class="code" href="class_database.htm#a2d8dad3df4134306749582216bd437e0">update</a>(<span class="stringliteral">&quot;table1&quot;</span>)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(<span class="stringliteral">&quot;a&quot;</span>, 123)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(<span class="stringliteral">&quot;b&quot;</span>, 333)</div><div class="line">         .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(QVariantMap{{<span class="stringliteral">&quot;a&quot;</span>, 1}, {<span class="stringliteral">&quot;b&quot;</span>, 2}})</div><div class="line">         .where(<span class="stringliteral">&quot;c=? AND d=?&quot;</span>, 3, 4);</div><div class="line"></div><div class="line">      qDebug() &lt;&lt; res2.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#accc70f0c2199629ce062feb8de0c2fe7">lastQuery</a>() &lt;&lt; res2.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#a4e82dd968f0b371514243e825a5b7f86">executedQuery</a>();</div><div class="line"></div><div class="line">      t.<a class="code" href="class_transaction.htm#a7388031cba0416d48e03e3f7acd5ca6a">commit</a>();</div><div class="line"></div><div class="line">      qDebug() &lt;&lt; <span class="stringliteral">&quot;end&quot;</span>;</div><div class="line">   }</div><div class="line">   <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm">DBException</a> &amp;e)</div><div class="line">   {</div><div class="line">      qDebug() &lt;&lt; e.<a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm#acae3cc68633e4be58440ec745d51cedb">lastError</a> &lt;&lt; e.<a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm#afc48e67740faefa6825adf04908d5924">lastQuery</a>;</div><div class="line">   }</div><div class="line"></div><div class="line">   qDebug() &lt;&lt; <span class="stringliteral">&quot;done&quot;</span>;</div><div class="line"></div><div class="line">   <span class="keywordflow">return</span> a.exec();</div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="whatsnext"></a>
What's next?</h1>
<p>You can:</p><ul>
<li>Read the documentation</li>
<li>Explore and run Qt tests</li>
<li>Explore the source code of the library</li>
<li>Use the library in your project</li>
<li>Give me feedback</li>
</ul>
<h1><a class="anchor" id="license"></a>
License</h1>
<pre class="fragment">
  The MIT License (MIT)
  Copyright 2018 Alexey Kramin
 
  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:
 
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
</pre> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
